package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;

import com.itheima.dao.RoleDao;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.HashMap;
import java.util.Map;


@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;


    @Override
    public List<Integer> findByUserId(int id) {
        return roleDao.findRoleIdsByUserId(id);
    }

    public void deleteByRoleId(Integer RoleId) {
        int count = roleDao.findRoleAndPermissionByRoleId(RoleId);
        if (count > 0) {
            roleDao.deleteRoleAndPermissionByRoleId(RoleId);
        }
        roleDao.deleteRoleByRoleId(RoleId);
    }

    @Override
    public List<Role> findAll() {
        List<Role> roles = roleDao.findAll();
        return roles;
    }

    @Override
    public List<Integer> findPermissionIdByRoleId(Integer roleId) {
        List<Integer> permissionIds = roleDao.findPermissionIdByRoleId(roleId);
        return permissionIds;
    }

    @Override
    public void edit(Role role, Integer[] permissionIds) {
        roleDao.editRole(role);
        roleDao.deleteRoleAndPermissionByRoleId(role.getId());
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap();
                map.put("roleId", role.getId());
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.findByCondition(queryString);
        long total = page.getTotal();
        List result = page.getResult();
        PageResult pageResult = new PageResult(total, result);
        return pageResult;
    }

    @Override
    public void add(Role role, Integer[] permissionIds) {
        roleDao.add(role);
        System.out.println(role.getId());
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap();
                map.put("roleId", role.getId());
                map.put("permissionId", permissionId);
                roleDao.setRoleAndPermission(map);
            }
        }
    }

    @Override
    public Role findRoleByRoleId(Integer roleId) {
        return roleDao.findRoleByRoleId(roleId);
    }
}